package com.sheyu.mall.coupon.service.impl;

import com.sheyu.mall.coupon.entity.SeckillSkuRelationEntity;
import com.sheyu.mall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sheyu.common.utils.PageUtils;
import com.sheyu.common.utils.Query;

import com.sheyu.mall.coupon.dao.SeckillSessionDao;
import com.sheyu.mall.coupon.entity.SeckillSessionEntity;
import com.sheyu.mall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    SeckillSkuRelationService seckillSkuRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLates3DaySession() {
        List<SeckillSessionEntity> seckillSessionEntities = this.baseMapper.selectList(
                new QueryWrapper<SeckillSessionEntity>().between("start_time", startTime(), endTime()));
        if (seckillSessionEntities!=null&&seckillSessionEntities.size()>0){
            seckillSessionEntities = seckillSessionEntities.stream().map(seckillSessionEntity -> {
                Long id = seckillSessionEntity.getId();
                List<SeckillSkuRelationEntity> seckillSkuRelationEntities =
                        seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>()
                                .eq("promotion_session_id", id));
                seckillSessionEntity.setSeckillSkuRelationEntities(seckillSkuRelationEntities);
                return seckillSessionEntity;
            }).collect(Collectors.toList());
            return seckillSessionEntities;
        }
        return null;
    }

    public  String startTime(){
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime startTime = LocalDateTime.of(now, min);
        //格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startFormat = formatter.format(startTime);
        return startFormat;
    }


    public static String endTime(){
        LocalDate now = LocalDate.now();
        now = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime localDateTime = LocalDateTime.of(now, max);

        //格式化时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String endFromat = formatter.format(localDateTime);
        return endFromat;
    }

    public static void main(String[] args) {
        System.out.println(endTime());
    }

}
